package com.ruoyi.erp.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.domain.vo.InventoryTypeVo;
import com.ruoyi.erp.domain.bo.InventoryTypeBo;
import com.ruoyi.erp.service.IInventoryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 物料类型Controller
 *
 * @author chenyun
 * @date 2022-03-06
 */
@Validated
@Api(value = "物料类型控制器", tags = {"物料类型管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/inventoryType")
public class InventoryTypeController extends BaseController {

    private final IInventoryTypeService iInventoryTypeService;

    /**
     * 查询物料类型列表
     */
    @ApiOperation("查询物料类型列表")
    @PreAuthorize("@ss.hasPermi('base:inventoryType:list')")
    @GetMapping("/list")
    public AjaxResult<List<InventoryTypeVo>> list(@Validated(QueryGroup.class) InventoryTypeBo bo) {
        List<InventoryTypeVo> list = iInventoryTypeService.queryList(bo);
        return AjaxResult.success(list);
    }

    /**
     * 导出物料类型列表
     */
    @ApiOperation("导出物料类型列表")
    @PreAuthorize("@ss.hasPermi('base:inventoryType:export')")
    @Log(title = "物料类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated InventoryTypeBo bo, HttpServletResponse response) {
        List<InventoryTypeVo> list = iInventoryTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料类型", InventoryTypeVo.class, response);
    }

    /**
     * 获取物料类型详细信息
     */
    @ApiOperation("获取物料类型详细信息")
    @PreAuthorize("@ss.hasPermi('base:inventoryType:query')")
    @GetMapping("/{id}")
    public AjaxResult<InventoryTypeVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iInventoryTypeService.queryById(id));
    }

    /**
     * 新增物料类型
     */
    @ApiOperation("新增物料类型")
    @PreAuthorize("@ss.hasPermi('base:inventoryType:add')")
    @Log(title = "物料类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody InventoryTypeBo bo) throws Exception {
        return toAjax(iInventoryTypeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改物料类型
     */
    @ApiOperation("修改物料类型")
    @PreAuthorize("@ss.hasPermi('base:inventoryType:edit')")
    @Log(title = "物料类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody InventoryTypeBo bo) throws Exception{
        return toAjax(iInventoryTypeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除物料类型
     */
    @ApiOperation("删除物料类型")
    @PreAuthorize("@ss.hasPermi('base:inventoryType:remove')")
    @Log(title = "物料类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iInventoryTypeService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
